package com.yunji.order.command.retund.impl;

import com.yunji.common.model.dto.LoginInfo;
import com.yunji.common.utils.TimeUtils;
import com.yunji.order.bo.PayableBO;
import com.yunji.order.business.WechatBusiness;
import com.yunji.order.command.retund.AbsRefundCommand;
import com.yunji.order.domain.OrderDO;
import com.yunji.order.domain.RefundDO;
import com.yunji.order.dto.cmd.RefundPassCommandDTO;
import com.yunji.order.emuns.RefundStatusEnum;
import com.yunji.order.wechat.WechatRefund;
import com.yunji.system.context.SystemContext;
import org.springframework.stereotype.Component;

/**
 * @author : peter-zhu
 * @date : 2025/3/28 11:27
 * @description : TODO
 **/
@Component
public class RefundPassCommand extends AbsRefundCommand<RefundPassCommandDTO, Boolean> {
    @Override
    public Boolean execute(RefundPassCommandDTO cmd) {
        RefundDO refundDO = cmd.getRefundDO();
        OrderDO order = orderService.getOrderById(refundDO.getOrderId());
        WechatRefund wechatRefund = wechatBusiness.tradeRefund(
                new PayableBO(order.getOrderCode(), refundDO.getRefundCode()
                        , WechatBusiness.multiply100(order.getActualAmount()), WechatBusiness.multiply100(refundDO.getRefundAmount())));
        boolean success = wechatRefund.isSuccess();
        RefundDO update = new RefundDO();
        update.setAuditBackStock(cmd.getAuditBackStock());
        update.setRefundId(refundDO.getRefundId());
        update.setApplyStatus(success ? RefundStatusEnum.REFUNDING.getCode() : RefundStatusEnum.FAILED.getCode());
        LoginInfo user = SystemContext.getUser();
        update.setAuditor(user.getUserId());
        update.setAuditName(user.getRealName());
        update.setAuditTime(TimeUtils.getNowDate());
        refundService.updateRefund(update);
        return success;
    }
}
